package com.cam.iot.mqtt.config;

import com.cam.iot.mqtt.handler.MqttChannelHandler;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

@Configuration
@ConfigurationProperties(prefix = "mqtt")
public class MqttConfig {

    @Value("${mqtt.host}")
    private String host;

    @Value("${mqtt.port}")
    private Integer port;

    @Value("${mqtt.username}")
    private String username;

    @Value("${mqtt.password}")
    private String password;

    @Value("${mqtt.client-id}")
    private String clientId;

    @Value("${mqtt.topics}")
    private String topics;
//    private List<String> topics;

    @Autowired
    private MqttChannelHandler mqttChannelHandler;

    @Bean
    public MqttPahoClientFactory factory() {
        System.out.println("初始化mqtt链接");

        String serverUri = "tcp://" + host + ":" + port;

        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions options = new MqttConnectOptions();
        options.setUserName(username);
        options.setPassword(password.toCharArray());
        options.setServerURIs(new String[]{serverUri});

        factory.setConnectionOptions(options);
        return factory;
    }

    @Bean
    public MessageChannel mqttInboundChannel() {
        return new DirectChannel();
    }

    @Bean
    public MessageProducer mqttChannelInbound(MessageChannel mqttInboundChannel, MqttPahoClientFactory factory) {
        String clientId = this.clientId + "_" + System.currentTimeMillis();
        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(clientId, factory, topics.split(","));
        adapter.setCompletionTimeout(5000);
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(0);
        adapter.setOutputChannel(mqttInboundChannel);
        return adapter;
    }

    /*@Bean
    public IntegrationFlow mqttInFlow(MqttPahoMessageDrivenChannelAdapter inboundAdapter) {
        return IntegrationFlows.from(inboundAdapter)
                .handle(message -> {
                    String payload = message.getPayload().toString();
                    String receivedTopic = message.getHeaders().get("mqtt_receivedTopic").toString();
                    System.out.println("Received message: " + payload + " from topic: " + receivedTopic);
                    // 这里添加业务处理逻辑
                }).get();
    }*/

    @Bean
    @ServiceActivator(inputChannel = "mqttInboundChannel")
    public MessageHandler mqttHandler() {
        return mqttChannelHandler;
    }

}
